/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.disparity;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.ArrayUtils;

/**
 *
 * @author aboulton
 */
public class CrackingEngine {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try {
            // TODO code application logic here
            File file1 = new File("c:\\junk\\Adam1.txt");
            byte[] f1Data = FileUtils.readFileToByteArray(file1);

            File orig = new File("c:\\junk\\Adam2.txt");

            byte[] file2 = FileUtils.readFileToByteArray(orig);

            System.out.println("[*] This is how the original file looks: " + f1Data.length);
            System.out.println("[*] This is how the patched file looks: " + file2.length);

            Map<Integer, Byte[]> diffs = CompareFiles.compare(f1Data, file2);

            for (Integer offset : diffs.keySet()) {

                //        System.out.println("[*] Offset: " + offset);
                Byte[] data = diffs.get(offset);

                //      System.out.println(Arrays.toString(data));
                System.out.println("[*] Patching offset: " + offset + " with " + ArrayUtils.toString(data));
                PatchingEngine.writeByteArrayToFile(file1, diffs);
            }

            //We can chop off the rest of the original file.
            if (file1.length() > file2.length) {
                FileChannel outChan = new FileOutputStream(file1, true).getChannel();
                long newSize = file2.length;
                outChan.truncate(newSize);
                outChan.close();
            }


        } catch (IOException ex) {
            Logger.getLogger(CrackingEngine.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
